package com.woniuxy.joylife.manager.controller;

import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.common.validate.AddGroup;
import com.woniuxy.common.validate.UpdateGroup;
import com.woniuxy.joylife.manager.entity.CarEntity;
import com.woniuxy.joylife.manager.service.CarService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 车辆类型信息
 *
 * @author renxiang
 * @email 981405195@qq.com
 * @date 2020-05-13 02:47:54
 */
@RestController
@RequestMapping("manager/car")
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:car:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = carService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{carId}")
    //@RequiresPermissions("manager:car:info")
    public R info(@PathVariable("carId") String carId){
		CarEntity car = carService.getById(carId);

        return R.ok().put("car", car);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manager:car:save")
    public R save(@Validated({AddGroup.class}) @RequestBody CarEntity car){
        car.setCarId(KeyUtil.generateKey());
        car.setState(0);
        car.setCreatetime(new Date());
		carService.save(car);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manager:car:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody CarEntity car){
        car.setUpdatetime(new Date());
		carService.updateById(car);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manager:car:delete")
    public R delete(@RequestBody String[] carIds){
		carService.removeByIds(Arrays.asList(carIds));

        return R.ok();
    }

}
